package com.yungu.swift.admin.thread;

import com.yungu.swift.autoconfig.redis.RedisCacheService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.OrderConstant;
import com.yungu.swift.lbs.model.DriverLocation;
import com.yungu.swift.lbs.model.OrderEventPoint;
import com.yungu.swift.lbs.service.LocationService;
import com.yungu.swift.order.model.vo.AdminMapTodayOrderVo;
import com.yungu.swift.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * Description:  今日订单线程
 *
 * @author Zerrion
 * @date 2018/11/13
 * @version: 1.0
 */
@Slf4j
public class OrderTodayThread implements Runnable {

    private AdminMapTodayOrderVo mapTodayOrderVo;
    private RedisCacheService redisCacheService;
    private LocationService locationService;
    private CountDownLatch countDownLatch;

    public OrderTodayThread(RedisCacheService cacheService, LocationService locationService, AdminMapTodayOrderVo mapTodayOrderVo, CountDownLatch countDownLatch) {
        this.mapTodayOrderVo = mapTodayOrderVo;
        this.redisCacheService = cacheService;
        this.locationService = locationService;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        // 1:待接单
        if ((OrderConstant.ORDER_MAIN_STATUS_INITIAL + "").equals(mapTodayOrderVo.getStatus())) {
            mapTodayOrderVo.setStatusName("待接单");
        }
        // 2:订单进行中
        if ((OrderConstant.ORDER_MAIN_STATUS_DOING + "").equals(mapTodayOrderVo.getStatus())) {
            mapTodayOrderVo.setStatusName("进行中");
            ResponseData<List<OrderEventPoint>> responseData = locationService.listOrderEventPoint(mapTodayOrderVo.getOrderUuid());
            if (responseData.isSuccess() && CollectionUtils.isNotEmpty(responseData.getData())) {
                List<OrderEventPoint> orderEventPointList = responseData.getData();
                orderEventPointList.forEach(orderEventPoint -> {
                    //位置信息类型  SJSB 司机上班,SJXB 司机下班,CKSC 乘客上车,CKXC 乘客下车,AUTO 定时上传,DDPD派单
                    if ("SJCF".equals(orderEventPoint.getPositionType())) {
                        mapTodayOrderVo.setOriginAddressName(orderEventPoint.getOrderEventAddress().getAddress());
                    }
                });
                //保存订单状态点
                mapTodayOrderVo.setOrderEventPointList(orderEventPointList);
            }

            //这边司机在实时上报位置，不能缓存
            List<DriverLocation> driverLocationList = locationService.findOrderMapPoint(mapTodayOrderVo.getOrderUuid(),
                    mapTodayOrderVo.getAppid()).getData();
            if (CollectionUtils.isNotEmpty(driverLocationList)) {
                //保存订单全部点
                mapTodayOrderVo.setOrderPointList(driverLocationList);
                //取得最后一个点来作为当前的点
                DriverLocation lastLocation = driverLocationList.get(driverLocationList.size() - 1);
                mapTodayOrderVo.setDestAddressName(locationService.getAddress(lastLocation.getCoordinate().getLng(),
                        lastLocation.getCoordinate().getLat()).getData());

            }

        }
        // 3-待支付 4:已支付
        if ((OrderConstant.ORDER_MAIN_STATUS_DONE + "").equals(mapTodayOrderVo.getStatus())
                || (OrderConstant.ORDER_MAIN_STATUS_PAYED + "").equals(mapTodayOrderVo.getMainStatus())) {
            //已完成
            mapTodayOrderVo.setStatusName("已完成");

            //先取缓存，缓存取不到在走查询逻辑
            String mapOriginAddressNameKey = "map_originAddressName_" + mapTodayOrderVo.getOrderUuid() + "_" + mapTodayOrderVo.getMainStatus();
            String mapDestAddressNameKey = "map_destAddressName_" + mapTodayOrderVo.getOrderUuid() + "_" + mapTodayOrderVo.getMainStatus();
            String mapOrderEventPointListKey = "map_orderEventPointList_" + mapTodayOrderVo.getOrderUuid() + "_" + mapTodayOrderVo.getMainStatus();
            Object originAddressNameCache = redisCacheService.get(mapOriginAddressNameKey);
            Object destAddressNameKeyCache = redisCacheService.get(mapDestAddressNameKey);
            Object orderEventPointListCache = redisCacheService.get(mapOrderEventPointListKey);
            if (orderEventPointListCache != null) {
                mapTodayOrderVo.setOrderEventPointList((List<OrderEventPoint>) orderEventPointListCache);
                if (originAddressNameCache != null) {
                    mapTodayOrderVo.setOriginAddressName(originAddressNameCache.toString());
                }
                if (destAddressNameKeyCache != null) {
                    mapTodayOrderVo.setDestAddressName(destAddressNameKeyCache.toString());
                }
            } else {
                List<OrderEventPoint> orderEventPointList = locationService.listOrderEventPoint(mapTodayOrderVo.getOrderUuid()).getData();
                if (CollectionUtils.isNotEmpty(orderEventPointList)) {
                    for (OrderEventPoint orderEventPoint : orderEventPointList) {
                        if ("SJCF".equals(orderEventPoint.getPositionType())) {
                            mapTodayOrderVo.setOriginAddressName(orderEventPoint.getOrderEventAddress().getAddress());
                            redisCacheService.set(mapOriginAddressNameKey, orderEventPoint.getOrderEventAddress().getAddress(), 10L);
                        }
                        if ("CKXC".equals(orderEventPoint.getPositionType())) {
                            mapTodayOrderVo.setDestAddressName(orderEventPoint.getOrderEventAddress().getAddress());
                            redisCacheService.set(mapDestAddressNameKey, orderEventPoint.getOrderEventAddress().getAddress(), 10L);
                        }
                    }
                    //已支付时 生成支付时间信息
                    if(mapTodayOrderVo.getPayTime()!=null){
                        OrderEventPoint payEvent = new OrderEventPoint();
                        //订单支付
                        payEvent.setPositionType("DDZF");
                        payEvent.setUploadTime(mapTodayOrderVo.getPayTime());
                        orderEventPointList.add(payEvent);
                    }

                    mapTodayOrderVo.setOrderEventPointList(orderEventPointList);
                    redisCacheService.set(mapOrderEventPointListKey, orderEventPointList, 10L);
                }
            }

            String driverLocationListKey = "map_driverLocationList_" + mapTodayOrderVo.getOrderUuid() + "_" + mapTodayOrderVo.getMainStatus();
            //先查询缓存
            Object driverLocationListCacheString = redisCacheService.get(driverLocationListKey);
            if (driverLocationListCacheString != null) {
                try {
                    mapTodayOrderVo.setOrderPointList(JsonUtils.readListValue(driverLocationListCacheString.toString(), DriverLocation.class));
                } catch (IOException e) {
                    log.error("OrderTodayThread readListValue error.driverLocationListCacheString:{}", driverLocationListCacheString, e);
                }
            } else {
                List<DriverLocation> driverLocationList = locationService.findOrderMapPoint(mapTodayOrderVo.getOrderUuid(),
                        mapTodayOrderVo.getAppid()).getData();
                if (driverLocationList != null) {
                    //保存订单全部点
                    mapTodayOrderVo.setOrderPointList(driverLocationList);
                    redisCacheService.set(driverLocationListKey, JsonUtils.toJSONString(driverLocationList), 10L);
                }
            }
        }
        // 5: 已取消
        if ((OrderConstant.ORDER_MAIN_STATUS_CANCEL + "").equals(mapTodayOrderVo.getStatus())) {
            mapTodayOrderVo.setStatusName("已取消");
        }

        countDownLatch.countDown();
    }
}
